﻿using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace template_dll
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        public SqlConnection conn;

        public string nameForm = "";
        public string nameAct = "";
        public int typePrize = 0;
        public int typeAct = 0;
        public DateTime dateStart = DateTime.Now.Date;
        public DateTime dateEnd = DateTime.Now.Date;
        public bool destr = false;
        private bool dateflag = false;

        private void Form3_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = nameForm;
                textBox1.Text = nameAct;
                if (dateStart == dateEnd && dateEnd == DateTime.Now.Date)
                {
                    DateTime now = DateTime.Now.AddDays(-1);
                    dateTimePicker1.Value = new DateTime(now.Year, now.Month, 1);
                    dateTimePicker2.Value = new DateTime(now.Year, now.Month, new DateTime(now.Year, now.Month, 1).AddMonths(1).AddDays(-1).Day);
                }
                else
                {
                    dateTimePicker1.Value = dateStart;
                    dateTimePicker2.Value = dateEnd;
                    dateflag = true;
                }
                if (destr) radioButton2.Checked = true;
                else radioButton1.Checked = true;

                SqlStorProc.sp_params[] param = new SqlStorProc.sp_params[0];
                
                foreach (DataRow row in new SqlStorProc().SqlProcExecWithQueryOneTable(conn, "pas_Actions_ActionTypes", 30, param).Rows)
                {
                    NewItem ni = new NewItem();
                    ni.ParamID = row["ActionTypeID"];
                    ni.ParamComment = row["ActionTypeName"].ToString();

                    comboBox1.Items.Add(ni);
                }

                foreach (DataRow row in new SqlStorProc().SqlProcExecWithQueryOneTable(conn, "pas_Actions_PrizeTypes", 30, param).Rows)
                {
                    NewItem ni = new NewItem();
                    ni.ParamID = row["PrizeTypeID"];
                    ni.ParamComment = row["PrizeTypeName"].ToString();

                    comboBox2.Items.Add(ni);
                }

                if (typeAct > 0)
                {
                    for (int i = 0; i < comboBox1.Items.Count; i++)
                        if (typeAct == Convert.ToInt32((comboBox1.Items[i] as NewItem).ParamID))
                            comboBox1.SelectedIndex = i;
                    comboBox1.Enabled = false;
                }

                if (typePrize > 0)
                {
                    for (int i = 0; i < comboBox2.Items.Count; i++)
                        if (typePrize == Convert.ToInt32((comboBox2.Items[i] as NewItem).ParamID))
                            comboBox2.SelectedIndex = i;
                    if (comboBox2.SelectedIndex > -1 && dateTimePicker1.Value < DateTime.Now.Date) comboBox2.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool check = false;
            if (textBox1.Text == "")
            {
                MessageBox.Show("Не заполнено наименование акции");
                textBox1.Focus();
            }
            else
                if (comboBox1.SelectedIndex == -1)
                {
                    MessageBox.Show("Не выбран тип акции");
                    comboBox1.Focus();
                }
                else
                    if (dateTimePicker1.Value >= dateTimePicker2.Value)
                    {
                        MessageBox.Show("Дата начала акции не может быть больше даты окончания");
                        dateTimePicker2.Focus();
                    }
                    else
                        if (dateTimePicker1.Value >= dateTimePicker2.Value)
                        {
                            MessageBox.Show("Дата начала акции не может быть больше даты окончания");
                            dateTimePicker2.Focus();
                        }
                        else
                            if (radioButton2.Checked)
                            {
                                if (MessageBox.Show("Подтвердите выбор", "Акция выключена", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                                    radioButton2.Focus();
                                else check = true;
                            }
                            else check = true;
            if (check && dateflag)
            {
                if (dateTimePicker1.Value != dateStart || dateTimePicker2.Value != dateEnd)
                    if (MessageBox.Show("Изменение периода акции может привести к потере данных планирования.\nПродолжить?", "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                        check = false;
            }
            if (check)
            {
                nameAct = textBox1.Text.Replace('(','[').Replace(')',']');
                dateStart = dateTimePicker1.Value;
                dateEnd = dateTimePicker2.Value;
                if (radioButton2.Checked) destr = true;
                else destr = false;
                typeAct = Convert.ToInt32((comboBox1.Items[comboBox1.SelectedIndex] as NewItem).ParamID);
                if(comboBox2.SelectedIndex > -1) typePrize = Convert.ToInt32((comboBox2.Items[comboBox2.SelectedIndex] as NewItem).ParamID);
                this.DialogResult = DialogResult.OK;
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '(' || e.KeyChar == ')')
            {
                if (e.KeyChar != (char)Keys.Back) e.Handled = true;
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Value = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, 1);
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker2.Value = new DateTime(dateTimePicker2.Value.Year, dateTimePicker2.Value.Month, new DateTime(dateTimePicker2.Value.Year, dateTimePicker2.Value.Month, 1).AddMonths(1).AddDays(-1).Day);
        }
    }
}
